package com.medata.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.*;

/**
 * @author ：zb
 * @date ：Created in 2019/10/15 0015 10:46
 * @description：
 */

public class TokenUtils {
    public static String tokenGet(String username,String limits){
        Map<String,Object> map=new HashMap<>();
        map.put("alg","HS256");
        map.put("typ","JWT");
        try {
            Algorithm algorithm=Algorithm.HMAC256("metadata_zb");
            String token = JWT.create()
                    .withHeader(map)
                    /*设置 载荷 Payload*/
                    .withClaim("loginName", username)
                    .withClaim("limits",limits)
                    //设置过期时间
                    .withExpiresAt(new Date(System.currentTimeMillis()+3600000*24))
                    .withIssuer("SERVICE")//签名是有谁生成 例如 服务器
                    .withSubject("this is test token")//签名的主题
                    //.withNotBefore(new Date())//该jwt都是不可用的时间
                    .withAudience("APP")//签名的观众 也可以理解谁接受签名的
                    /*签名 Signature */
                    .sign(algorithm);
            return token;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String validToken(String token, String dbid){
        try {
            Algorithm algorithm = Algorithm.HMAC256("metadata_zb");
            JWTVerifier verifier = JWT.require(algorithm)
                    .withIssuer("SERVICE")
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            String subject = jwt.getSubject();
            List<String> audience = jwt.getAudience();
            Map<String, Claim> claims = jwt.getClaims();
            Claim limits = claims.get("limits");
            //验证操作权限
            String ss = limits.asString();
            String[] split = ss.split(",");
            Set<String> set=new HashSet<>(Arrays.asList(split));
            int size = set.size();
            String[] dbids = dbid.split(",");
            for (String did:dbids){
                if (!did.isEmpty()){
                    set.add(did);
                }
            }
            if (set.size()!=size){
                return null;
            }else {
                Claim name = claims.get("loginName");
                return name.asString();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static String validToken2(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256("metadata_zb");
            JWTVerifier verifier = JWT.require(algorithm)
                    .withIssuer("SERVICE")
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            Map<String, Claim> claims = jwt.getClaims();
            Claim name = claims.get("loginName");
            return name.asString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String stoken="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIHRlc3QgdG9rZW4iLCJhdWQiOiJBUFAiLCJsb2dpbk5hbWUiOiJ6YiIsImlzcyI6IlNFUlZJQ0UiLCJleHAiOjE1NzMwMzE0MjEsImxpbWl0cyI6IjEsMiwzIn0.AUV8WK_xh3AC7Iscw0-R-6t2b3i0hqzkSG8hgkZ_69k";
        System.out.println(validToken(stoken,"1"));
        System.out.println(tokenGet("zb","1,2,3"));
    }
}
